package PersistenciaDAOs;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import SQLQrys.QrysChorro;
import SQLQrys.QrysJugador;
import SQLQrys.QrysLancha;
import SQLQrys.QrysParametros;
import logica.Chorro;
import logica.Lancha;
import exceptions.PersistenciaException;
import logica.ConexionBD;


public class DAOLancha {
		
	private ConexionBD DBC;
	private Lancha[] lanchas;
	private DAOMapa daoMap;
	
    public DAOLancha(){
}
	
public void insert(Lancha Lan)  throws PersistenciaException{
	String ins=null;

try{
	QrysLancha cons = new QrysLancha();
	ins = cons.insertLancha();
	
	PreparedStatement pstmt1;
	DBC=new ConexionBD();
	
	pstmt1 = DBC.getConnection().prepareStatement(ins);
	pstmt1.setInt(1, Lan.getId());
	pstmt1.setInt(2, Lan.getIdPartida());
	pstmt1.setInt(3, Lan.getPtoInicioX());
	pstmt1.setInt(4, Lan.getPtoInicioY());
	pstmt1.setInt(5, Lan.getVida());
	
	pstmt1.executeUpdate();
	//cierro preparedStatement.
	pstmt1.close();
}catch (SQLException e) {
	e.printStackTrace();
	throw new PersistenciaException("Se ha producido un error al intentar guardar la lancha");
}finally{
	DBC.desconectar();
	}
}

public void update(Lancha Lan) throws PersistenciaException{
	String upd=null;
	try {
		
		QrysLancha cons = new QrysLancha();
		upd=cons.updateLancha();
		PreparedStatement pstmt1;
		DBC=new ConexionBD();
		pstmt1 = DBC.getConnection().prepareStatement(upd);

		pstmt1.setInt(1, Lan.getPtoInicioX());		
		pstmt1.setInt(2, Lan.getPtoInicioY());
		pstmt1.setInt(3, Lan.getVida());
		pstmt1.setInt(4, Lan.getId());
		pstmt1.setInt(5, Lan.getIdPartida());
		pstmt1.executeUpdate();
		//cierro preparedStatement.
		pstmt1.close();
	} catch (SQLException e) {
		e.printStackTrace();
		throw new PersistenciaException("Se ha producido un error al intentar guardar lancha");
	}finally{
		DBC.desconectar();
	}	
}

public boolean member(int idPart){
	String sel=null;
	boolean mem = true;
	
	try{
		DBC=new ConexionBD();
		QrysLancha cons=new QrysLancha();
		
		sel = cons.selectCount();
		PreparedStatement pstmt1;
		pstmt1 = DBC.getConnection().prepareStatement(sel);;
		pstmt1.setInt(1, idPart);
		
		ResultSet rs = pstmt1.executeQuery();
		while(rs.next()){
			if(rs.getInt("cnt")==0){
				mem = false;
			}
			else{
				mem = true;
			}
		}	
		
		rs.close();
		pstmt1.close();
		
}catch (SQLException e) {
	e.printStackTrace();
	throw new PersistenciaException("Se ha producido un error al intentar buscar la Lancha");
	}finally{
		DBC.desconectar();
		return mem;
		}	
}

public Lancha[] selectLoad(int idPart) throws PersistenciaException{
	String selP=null;
	String sel=null;
	float	vel = 0, distBalas = 0, visi = 0;
	int cont = 0;
	
	lanchas = new Lancha[3];
	
	try{
		DBC=new ConexionBD();
		
		QrysParametros consP = new QrysParametros();
		selP = consP.getParametros();
		
		QrysLancha cons = new QrysLancha();
		sel = cons.selectLanchas();

		PreparedStatement pstmtP;
		PreparedStatement pstmt1;

		pstmtP = DBC.getConnection().prepareStatement(selP);
		ResultSet rs = pstmtP.executeQuery();

		while(rs.next()){
			vel = rs.getInt("velocidad") * rs.getFloat("vel_lancha");
			distBalas = rs.getInt("alcance") * rs.getFloat("alc_lancha");
			visi = rs.getInt("visibilidad") * rs.getFloat("visi_lancha");
		}
		
		pstmt1 = DBC.getConnection().prepareStatement(sel);
		pstmt1.setInt(1, idPart);
		
		ResultSet rs2 = pstmt1.executeQuery();
		while(rs2.next()){
			Lancha Lan = new Lancha();
			//System.out.print(rs2.getInt("id"));
			Lan.setId(rs2.getInt("id"));
			Lan.setIdPartida(rs2.getInt("id_partida"));
			Lan.setPtoInicioX(rs2.getInt("pto_ini_x"));
			Lan.setPtoInicioY(rs2.getInt("pto_ini_y"));
			Lan.setVida(rs2.getInt("vida"));
			Lan.setVelocidad(vel);
			Lan.setAlcance(distBalas);
			Lan.setVisibilidad(visi);
			
			lanchas[cont] = Lan;
			cont++;
		}
		
		return lanchas;
	}catch (SQLException e) {
		e.printStackTrace();
		throw new PersistenciaException("Se ha producido un error al intentar cargar lanchas");
	}finally{
		DBC.desconectar();
		}
}
		
public Lancha[] selectCreate (int idPart, int idMap) throws PersistenciaException{
	String selP=null;
	float vel = 0, distBalas = 0, visi = 0;
	int cont = 0, vida = 0;
	
	daoMap = new DAOMapa();
	
	lanchas = new Lancha[3];

	try{
		DBC=new ConexionBD();
		
		QrysParametros consP = new QrysParametros();
		selP = consP.getParametros();

		PreparedStatement pstmtP;
		
		pstmtP = DBC.getConnection().prepareStatement(selP);
		
		ResultSet rs = pstmtP.executeQuery();
		while(rs.next()){
			vel = rs.getInt("velocidad") * rs.getFloat("vel_lancha");
			distBalas = rs.getInt("alcance") * rs.getFloat("alc_lancha");
			visi = rs.getInt("visibilidad") * rs.getFloat("visi_lancha");
			vida = rs.getInt("vida");
		}
		

		ResultSet rs2 = pstmtP.executeQuery();
		while(cont < 3){
			Lancha Lan = new Lancha();
			Lan.setId(cont+1);
			Lan.setIdPartida(idPart);
			Lan.setVida(vida);
			Lan.setVelocidad(vel);
			Lan.setAlcance(distBalas);
			Lan.setVisibilidad(visi);
			lanchas[cont] = Lan;
			cont++;
		}
		
		
		lanchas[0].setPtoInicioX(daoMap.select(idMap).getPtoIncioXLancha1());
		lanchas[0].setPtoInicioY(daoMap.select(idMap).getPtoIncioYLancha1());
		lanchas[1].setPtoInicioX(daoMap.select(idMap).getPtoIncioXLancha2());
		lanchas[1].setPtoInicioY(daoMap.select(idMap).getPtoIncioYLancha2());
		lanchas[2].setPtoInicioX(daoMap.select(idMap).getPtoIncioXLancha3());
		lanchas[2].setPtoInicioY(daoMap.select(idMap).getPtoIncioYLancha3());
		
		return lanchas;
	}catch (SQLException e) {
		e.printStackTrace();
		throw new PersistenciaException("Se ha producido un error al intentar cargar lanchas");
	}finally{
		DBC.desconectar();
	}		
}

	

}

